package com.ruicar.afs.cloud.apply.business.mq.synchro;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.parameter.commom.entity.TsysVehicleDetail;
import com.ruicar.afs.cloud.parameter.commom.entity.TsysVehicleModel;
import com.ruicar.afs.cloud.parameter.commom.entity.VeBrand;
import com.ruicar.afs.cloud.parameter.commom.entity.VeMain;
import com.ruicar.afs.cloud.parameter.commom.service.TsysVehicleDetailService;
import com.ruicar.afs.cloud.parameter.commom.vo.VeMainVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Ice
 * @date 2020/9/16 14:15
 */
@Slf4j
@Component
@AllArgsConstructor
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit", name = "enable")
public class ApplyChannelSynchroMain implements AfsMqBizProcessor<VeMainVo> {
    private final TsysVehicleDetailService tsysVehicleDetailService;
    @Override
    public boolean processMessage(VeMainVo entity) throws Exception {

        if (ObjectUtil.isNotNull(entity)) {
            log.info("已成功接收到车型库MQ报文，解析报文中... ......(*￣11111111￣)ノ");
            this.getFileterParams(entity.getVeMainList());
        }
        return true;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.CHANNEL_CHANNEL_APPLY_POS_SEND_VEHICLE_MAIN_APPLY;
    }

    @Override
    public boolean reQueueOnFail() {
        return false;
    }


    /**
     * 解析MQ报文
     *
     * @param entity
     */
    public void getFileterParams(List<VeMain> entity) throws ParseException {
        DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(entity.size()>0){
            for (VeMain main:entity){
                TsysVehicleDetail detail =tsysVehicleDetailService.getOne(Wrappers.<TsysVehicleDetail>query().lambda()
                        .eq(TsysVehicleDetail::getCode,main.getCode()).eq(TsysVehicleDetail::getModelCode,main.getBrandcode()));
                if(detail==null){
                    TsysVehicleDetail t =new TsysVehicleDetail();
                    t.setModelCode(main.getBrandcode());
                    t.setCode(main.getCode());
                    t.setName(main.getName());
                    if(main.getGuideprice()!=null){
                        t.setGuidePrice(new BigDecimal(main.getGuideprice()));
                    }
                    t.setIsActive(main.getIsactive());
                    if(main.getOrderid()!=null&&main.getOrderid()!=""){
                        t.setOrderId(Integer.valueOf(main.getOrderid()));
                    }
                    t.setDisplacement(main.getVehiclemain05());
                    t.setTransmission(main.getVehiclemain06());
                    t.setConfigure(main.getVehiclemain07());
                    t.setYearStyle(main.getYearstyle());
                    t.setAssetsCode(main.getZichancode());
                    t.setPowerType(main.getDonglitype());
                    t.setDriver(main.getDriver());
                    t.setBlowoff(main.getPaifang());
                    t.setCmsId(main.getCmsid());
                    t.setRedBlue(main.getVemain01());
                    t.setMemo(main.getMemo());
                    t.setInterior(main.getVemain02());
                    t.setModelId(main.getModelid());
                    t.setChe300Style(main.getChe300style());
                    if(main.getChelong()!=null){
                        t.setCarLength(new BigDecimal(main.getChelong()));
                    }
                    if(main.getWight()!=null){
                        t.setWight(new BigDecimal(main.getWight()));
                    }
                    if(main.getSeatnum()!=null){
                        t.setSeatNum(new BigDecimal(main.getSeatnum()));
                    }
                    t.setCarLevel(main.getCarlevel());
                    t.setIsParallel(main.getIsparallel());
                    t.setNeedSync(main.getNeedsync());
                    t.setIsInternet(main.getIschelianwag());
                    t.setIsGreen(main.getIsgreen());
                    t.setCarType(main.getCartype());
                    t.setCarTypeDetail(main.getCartypedetail());
                    t.setUpdateBy(main.getUpdateuser());
                    t.setCreateBy(main.getCreateuser());
                    t.setCreateTime(new Date(System.currentTimeMillis()));
                    t.setUpdateTime(new Date(System.currentTimeMillis()) );
                    tsysVehicleDetailService.save(t);

                }else {
                    detail.setModelCode(main.getBrandcode());
                    detail.setCode(main.getCode());
                    detail.setName(main.getName());
                    if(main.getGuideprice()!=null){
                        detail.setGuidePrice(new BigDecimal(main.getGuideprice()));
                    }
                    detail.setIsActive(main.getIsactive());
                    if(main.getOrderid()!=null&&main.getOrderid()!=""){
                        detail.setOrderId(Integer.valueOf(main.getOrderid()));
                    }
                    detail.setDisplacement(main.getVehiclemain05());
                    detail.setTransmission(main.getVehiclemain06());
                    detail.setConfigure(main.getVehiclemain07());
                    detail.setYearStyle(main.getYearstyle());
                    detail.setAssetsCode(main.getZichancode());
                    detail.setPowerType(main.getDonglitype());
                    detail.setDriver(main.getDriver());
                    detail.setBlowoff(main.getPaifang());
                    detail.setCmsId(main.getCmsid());
                    detail.setRedBlue(main.getVemain01());
                    detail.setMemo(main.getMemo());
                    detail.setInterior(main.getVemain02());
                    detail.setModelId(main.getModelid());
                    detail.setChe300Style(main.getChe300style());
                    if(main.getChelong()!=null){
                        detail.setCarLength(new BigDecimal(main.getChelong()));
                    }
                    if(main.getWight()!=null){
                        detail.setWight(new BigDecimal(main.getWight()));
                    }
                    if(main.getSeatnum()!=null){
                        detail.setSeatNum(new BigDecimal(main.getSeatnum()));
                    }
                    detail.setCarLevel(main.getCarlevel());
                    detail.setIsParallel(main.getIsparallel());
                    detail.setNeedSync(main.getNeedsync());
                    detail.setIsInternet(main.getIschelianwag());
                    detail.setIsGreen(main.getIsgreen());
                    detail.setCarType(main.getCartype());
                    detail.setCarTypeDetail(main.getCartypedetail());
                    detail.setUpdateBy(main.getUpdateuser());
                    detail.setCreateBy(main.getCreateuser());
                    detail.setCreateTime(new Date(System.currentTimeMillis()));
                    detail.setUpdateTime(new Date(System.currentTimeMillis()) );
                    tsysVehicleDetailService.updateById(detail);
                }
            }
        }

    }
}
